'Triennial Review Requirement Script  
Option Strict Off
imports System
imports System.Collections
imports PHSRAG.Insight21.Humans
imports PHSRAG.Insight21.BusinessObjects
imports FormsManagement = PHSRAG.Insight21.FormsManagement

module Script
sub Main()

    Dim hazardExists as Boolean
    Dim tumorExists as Boolean
    Dim procExists as Boolean
    Dim anesExists as Boolean
    Dim sponsorExists as Boolean
    for each qa as FormsManagement.QuestionnaireAnswer in currentProtVersion.QuestionnaireAnswers
        select case qa.QuestionnaireCategory
            case "QUESTCAT_ANM_TP"
                tumorExists = true
            case "QUESTCAT_ANM_PRO"
                procExists = true
            case "QUESTCAT_ANM_HZ"
                hazardExists = true
            case "QUESTCAT_ANM_AN"
                anesExists = true
            case "QUESTCAT_ANM_SF"
                sponsorExists = true
        end select
    next

    Dim answers as System.Collections.ArrayList = currentProtVersion.QuestionnaireAnswers
    if answers.Count > 0 then
        Dim latestIRVersion as ProtocolVersion = Protocol.GetLatestApplicationVersion (theProtocol.Id, connectionString, false, cachePolicy)
		Dim oldAnswers as new ArrayList()
		if not latestIRVersion is nothing then
            oldAnswers = latestIRVersion.QuestionnaireAnswers
        end if   

        currentProcess.ShowAttachments = True
        dim triennialQAs as ArrayList = GetQuestionnaireAnswerByCategory("QUESTCAT_ANM_TRI")
        Dim isCreatedThrougheIACUC as ArrayList = triennialQAs(0).GetAnswers("Insight")
        if not isCreatedThrougheIACUC is nothing then
            if isCreatedThrougheIACUC.Count > 0 then
                if (isCreatedThrougheIACUC(0).Value = "N") then 
					' Standard always gets Abstract and Detailed Research Plan
					Dim abstractQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Abstract", FormsManagement.Constants.QuestionnaireType.Animals )
					Dim abstract as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(abstractQV.Id, cachePolicy, "Abstract", true)

					Dim detailedPlanQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Detailed Research Plan", FormsManagement.Constants.QuestionnaireType.Animals )
					Dim detailedPlan as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(detailedPlanQV.Id, cachePolicy, "Detailed Research Plan", true)

					' Standard Application
					Dim standardQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Standard Application", FormsManagement.Constants.QuestionnaireType.Animals )
					Dim standard as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(standardQV.Id, cachePolicy, "Standard Application", true)

					' Sponsor Application
					if not sponsorExists then 
					    Dim sponsorQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Sponsor Funding", FormsManagement.Constants.QuestionnaireType.Animals )
					    Dim sponsor as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(sponsorQV.Id, cachePolicy, "Sponsor Funding", true)
                    end if

					' Duration & clinical signs
					Dim durationClinicalSignsQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Duration, Clinical Signs, Endpoints and Euthanasia", FormsManagement.Constants.QuestionnaireType.Animals )
					Dim durationClinical as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(durationClinicalSignsQV.Id, cachePolicy, "Duration, Clinical Signs, Endpoints and Euthanasia", true)
                                                                                        
                    ' Housing
                    Dim housingQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Housing", FormsManagement.Constants.QuestionnaireType.Animals )
                    Dim housing as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(housingQV.Id, cachePolicy, "Housing", true)

                    ' Potential Pain and Distress
                    Dim ppdQV as FormsManagement.QuestionnaireVersion = FormsManagement.Questionnaire.GetActiveVersion( cachePolicy, "Potential Pain and Distress", FormsManagement.Constants.QuestionnaireType.Animals )
                    Dim ppd as FormsManagement.QuestionnaireAnswer = currentProtVersion.AddNewQuestionnaireAnswer(ppdQV.Id, cachePolicy, "Potential Pain and Distress", true)

					' All Animals protocols must have a flowchart attachement
					currentProcess.AddProtocolDocument ( currentProcess.ProtocolId, "Flowchart", "ATCHTYPE_FLOWCHART","Flowchart", false, currentProcess.Id )

                			currentProcess.ShowStaff = true
                else if (isCreatedThrougheIACUC(0).Value = "Y") then
                    currentProtVersion.DeleteQuestionnaireAnswer("Standard Application")
                    
					' Housing
                    currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, "Housing", "Housing", oldAnswers, false, theProtocol.Id)

					' Potential Pain and Distress
                    currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, "Potential Pain and Distress", "Potential Pain and Distress", oldAnswers, false, theProtocol.Id)

                    currentProcess.ShowStaff = (triennialQAs(0).GetAnswerValue("StaffChange") = "Y")
                    AddForm("Funding", "Y", "Sponsor Funding", oldAnswers, true, sponsorExists)
                    AddForm("Hazard", "Y", "Hazardous Agent Administration and Use", oldAnswers, true, hazardExists)
                    AddForm("Anesthesia", "N", "Anesthesia Regimen", oldAnswers, true, anesExists)
                    AddForm("Procedures", "N", "Procedures", oldAnswers, true, procExists)
                    AddForm("AbstractYes", "Y", "Abstract", oldAnswers, false, false)
                    AddForm("DetailYes", "Y", "Detailed Research Plan", oldAnswers, false, false)
                    AddForm("AbstractYes", "Y", "Abstract", oldAnswers, false, false)
                    AddForm("Endpoints", "N", "Duration, Clinical Signs, Endpoints and Euthanasia", oldAnswers, false, false)
                    

                    AddForm("Special", "ASCITES", "Antibody & Ascites Production", oldAnswers, false, false)
                    AddForm("Special", "TUMOR", "Tumor Production", oldAnswers, true, tumorExists)
                    AddForm("Special", "TRANNON", "Transportation", oldAnswers, false, false)
                    AddForm("Special", "TRAN", "Transportation", oldAnswers, false, false)
                    AddForm("Special", "MXSX", "Multiple Survival procedures", oldAnswers, false, false)
                    AddForm("Special", "PARA", "Paralytics", oldAnswers, false, false)
                    AddForm("Special", "FOOD", "Controlled Food or Fluid Intake", oldAnswers, false, false)
                    AddForm("Special", "PAIN", "Pain and/or Distress Study Justification", oldAnswers, false, false)
                    AddForm("Special", "UNPAIN", "Pain and/or Distress Study Justification", oldAnswers, false, false)
                    AddForm("Special", "BREED", "Breeding Studies", oldAnswers, false, false)
                    AddForm("Special", "REST", "Restraint", oldAnswers, false, false)
                    
                    For each qa as FormsManagement.QuestionnaireAnswer in oldAnswers
                        if qa.Name = "Literature Search- Alternatives" or qa.Name = "Literature Search- Refine" then
                            currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, qa.Name, qa.Name, oldAnswers, false, theProtocol.Id)
                        end if
                    next
                end if
            end if
        end if
        
        theProtocol.Save( connectionString )
        currentProtVersion.Save ( connectionString )
        currentProcess.Save( connectionString )
    end if

end sub  

' Adds or removes questionnaire answers to protocol version based on answer to a question, also copies previous forms
sub AddForm(byval answerName as string, byval answerValue as string, byval questionnaireName as string, byval oldAnswers as ArrayList, byval isMultiple as boolean, byval formExists as boolean)
    dim triennialQAs as ArrayList = GetQuestionnaireAnswerByCategory("QUESTCAT_ANM_TRI")
    Dim answers as ArrayList = triennialQAs(0).GetAnswers(answerName)
    Dim answerMatch as Boolean = false
    for each answer as FormsManagement.Answer in answers
        if answer.Value = answerValue then
            answerMatch = true
            exit for
        end if        
    next
    if answerMatch and not formExists then
        currentProtVersion.CreateNewQuestionnaireAnswer(connectionString, questionnaireName, questionnaireName, oldAnswers, isMultiple, theProtocol.Id)
    else if not answerMatch then
        currentProtVersion.DeleteQuestionnaireAnswer(questionnaireName)
    end if
end sub

'function to return the qAs based on the category passed in
function GetQuestionnaireAnswerByCategory(byval category as string) 
	dim qAs as new ArrayList()
	for each qa as FormsManagement.QuestionnaireAnswer in currentProtVersion.QuestionnaireAnswers
		if category =  qa.QuestionnaireCategory then
			dim str = qa.Name
			qAs.add(qa)
			'There might be more than one questionnaire answer that might be returned, so do not exit
		end if   
	next
	return qAs
end function

end module
